<template>
  <div>
    <div style="margin-bottom: 5px;">
      <el-button type="primary" style="margin-left: 5px;" @click="add">新增</el-button>
    </div>
    <el-table :data="tableData"
              :header-cell-style="{ background: '#f2f5fc', color: '#555555' }"

    >
      <el-table-column prop="id" v-if="false">
      </el-table-column>
      <el-table-column prop="account" label="账号" width="100">
      </el-table-column>
      <el-table-column prop="userName" label="用户名" width="100">
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="100">
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="60">
        <template slot-scope="scope">
          <el-tag
              :type="scope.row.gender === 1 ? 'primary' : 'success'"
              disable-transitions>{{scope.row.gender === 1 ? '男' : '女'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="lastTime" label="最后登录时间" width="150">
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
              style="margin-left: 5px;"
          >
            <el-button slot="reference" size="small" type="danger" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20,30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="添加好友"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>

      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-col :span="20">
            <el-input v-model="account"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doSave">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Friend",
  data() {
    let checkDuplicate =(rule,value,callback)=>{
      this.$axios.get(this.$httpUrl+"/sns-user/findByAccount?account="+this.account).then(res=>res.data).then(res=>{
        if(res.code==200){
          callback('该账号存在！')
          this.friend = res.data.user
        }else{
          callback(new Error('账号不存在'));
        }
      })
    };

    return {
      tableData: [],
      user:{},
      friend:{},
      pageSize:10,
      pageNum:1,
      total:0,
      mainId:'',
      account:'',
      centerDialogVisible:false,
      form:{
        id:'',
        mainId:'',
        friendId:''
      },
      rules: {
        account: [
          {validator:checkDuplicate,trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.init()
  },
  methods:{
    init(){
      this.user = JSON.parse(sessionStorage.getItem('CurUser'))
      this.mainId = this.user.id
    },
    resetForm() {
      this.account = ''
      this.$refs.form.resetFields();
    },
    del(id){
      console.log(id)

      this.$axios.get(this.$httpUrl+'/sns-friend/del?id='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){

          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost()
        }else{
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        }

      })
    },
    add(){
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        this.resetForm()
      })

    },
    doSave(){
      this.form.friendId = this.friend.id
      this.form.mainId = this.mainId
      this.$axios.post(this.$httpUrl+'/sns-friend/save',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){

          this.$message({
            message: '添加成功！',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else{
          this.$message({
            message: '添加失败！',
            type: 'error'
          });
        }

      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/sns-friend/listPage',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          mainId:this.user.id+''
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else{
          alert('获取数据失败')
        }

      })
    },
  },
  beforeMount() {
    this.loadPost()
  }
}
</script>

<style scoped>

</style>